<?php
class Util {
	public static function getBootstrapOptions()
	{
		$config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
		return $config;
	}

	public static function is_interger($int){
       
        // First check if it's a numeric value as either a string or number
        if(is_numeric($int) === TRUE){
           
            // It's a number, but it has to be an integer
            if((int)$int == $int){

                return TRUE;
               
            // It's a number, but not an integer, so we fail
            }else{
           
                return FALSE;
            }
       
        // Not a number
        }else{
       
            return FALSE;
        }
    }
    
    public static function is_valid_email($email)
    {
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
    }
    
	public static function get_include_contents($filename,$data='') {
	    if (is_file($filename)) {
	    	if (is_array($data)){
			extract($data);
	    	}
	        ob_start();
	        include $filename;
	        $contents = ob_get_contents();
	        ob_end_clean();
	        return $contents;
	    }
	    return false;
	}
	
	public static function get_time_now(){
    	return time();
    }
    
    public static function change_int_to_date($int){
    	return gmdate("d-m-Y", $int);
    }
    
    public static function change_date_to_int($month, $day, $year){
    	return mktime(0, 0, 0, $month, $day, $year)+7*3600;
    }
    
	public function makeDays($stime){
		
		/* ham chuyen doi thoi gian thanh chuoi thu,ngay,thang,nam,gio theo chuan vietnam */
		$_stime= $stime; // -7 tieng
		//$date = date("D",$_stime);
		/*
		switch ($date)
		{
 		    case "Mon":
		    $date = "Thứ hai";
		    break;
	   		case "Tue":
    		$date = "Thứ ba";
		    break;		 
 		    case "Wed":
		    $date = "Thứ tư°";
		    break;
	   		case "Thu":
    		$date = "Thứ năm";
		    break; 		    
		    case "Fri":
		    $date = "Thứ sáu";
		    break;
	   		case "Sat":
    		$date = "Thứ bảy";
		    break;
	   		case "Sun":
    		$date = "Chủ nhật";
		    break;
		}
		$date = $date." , ".date("d-m-Y , H:i A",$_stime);
		*/
		$date = date("d-m-Y , H:i:s",$_stime);
						
		return $date; 
	}
	
	public static function image_generator($data){
		$html = "";
	    		switch($data['align'])
	    		{
	    			case 1 : $align = "LEFT";break;
	    			case 2 : $align = "RIGHT";break;
	    			case 3 : $align = "TOP";break;
	    			case 4 : $align = "TEXTTOP";break;
	    			case 5 : $align = "MIDDLE";break;
	    			case 6 : $align = "ABSMIDDLE";break;
	    			case 7 : $align = "BOTTOM";break;
	    			case 8 : $align = "ABSBOTTOM";break;
	    			case 9 : $align = "BASELINE";break;
	    			default: $align = "";break;
	    		}
	    		switch($data['target'])
	    		{
	    			case 1 : $target = "_blank";break;
	    			case 2 : $target = "_top";break;
	    			case 3 : $target = "_self";break;
	    			case 4 : $target = "_parent";break;
	    			default: $target = "";break;
	    		}
	    		$img= '<img src="'.$data['src'].'" align="'.$align.'" border="'.$data['border'].'" height="'.$data['height'].'" width="'.$data['width'].'" hspace="'.$data['hspace'].'" vspace="'.$data['vspace'].'" style="border: '.$data['border'].'px solid black; margin: '.$data['vspace'].'px '.$data['hspace'].'px; " />';
	    		
	    		
	    $html.=$img;
		return $html;
	}
	
	public static function html_editor(){
    	$session = new Zend_Session_Namespace('block.auth');
		$html = "";
		
	 	for($i=1;$i<=sizeof($session->block);$i++)
	    {
	    	if($session->block[$i-1]['type'] == 3 || $session->block[$i-1]['type'] == 4)
	    		$html.= $session->block[$i-1]['data'];
	    	if($session->block[$i-1]['type'] == 1)
	    	{
	    		if($session->block[$i-1]['image']!= "")
	    		{
	    			foreach($session->block[$i-1]['image'] as $data)
	    			{
			    		switch($data['align'])
			    		{
			    			case 1 : $align = "LEFT";break;
			    			case 2 : $align = "RIGHT";break;
			    			case 3 : $align = "TOP";break;
			    			case 4 : $align = "TEXTTOP";break;
			    			case 5 : $align = "MIDDLE";break;
			    			case 6 : $align = "ABSMIDDLE";break;
			    			case 7 : $align = "BOTTOM";break;
			    			case 8 : $align = "ABSBOTTOM";break;
			    			case 9 : $align = "BASELINE";break;
			    			default: $align = "";break;
			    		}
			    		switch($data['target'])
			    		{
			    			case 1 : $target = "_blank";break;
			    			case 2 : $target = "_top";break;
			    			case 3 : $target = "_self";break;
			    			case 4 : $target = "_parent";break;
			    			default: $target = "";break;
			    		}
			    		$img= '<img src="'.$data['src'].'" alt="'.$data['alt'].'" align="'.$align.'" border="'.$data['border'].'" height="'.$data['height'].'" width="'.$data['width'].'" hspace="'.$data['hspace'].'" vspace="'.$data['vspace'].'" style="border: '.$data['border'].'px solid black; margin: '.$data['vspace'].'px '.$data['hspace'].'px;" />';
			    		if(trim($data['url'])!="")
			    			$img = '<a href="http://'.trim($data['url']).'" target="'.$target.'">'.$img.'</a>';
			    		
			    		$html.=$img;
	    			}
	    		}
	    		$html.= $session->block[$i-1]['data'];
	    	}
	    	$html.= '<div style="clear:both;"></div>';
	    }
	    
		return str_replace("\\", "", $html);
    }
    
	public static function cutStr($words){
		$k = 0;
		$result[0] = "";
		$vowels = array("&quot;","'","+","-","=","^");
		$words = str_replace($vowels," ",$words);
		$words = trim($words," ");
		$character = str_split($words);
		for ($i = 0;$i < strlen($words);$i++)
			{
				if($character[$i]!=" ")
					$result[$k].= $character[$i];
				if($character[$i]==" " && $character[$i+1]!=" ")
				{
					$k++;
					$result[$k] = "";
				}
			}
		return $result;
	}
	
	public static function highlight($str,$words){
		$cutStr = Util::cutStr($words);
		foreach($cutStr as $word)
		{
			$_str = " ".strtolower($str);
			$_word = strtolower($word);
			$len = strlen($word);
			$start = 0;
			$result ="";
			$end = strpos($_str,$_word);
			if($end=="") continue;
			while($end!="")
			{
				$result.= substr($str, $start, $end - 1);
				$result.='<b class="highlight yellow">';
				$result.= substr($str, $end - 1, $len);
				$result.='</b>';
				$str = substr($str, $end + $len - 1);
				$start = 0;
				$_str = " ".strtolower($str);
				$end = strpos($_str,$_word);
			}
			$result.= $str;
			$str = $result;
		}
		if($result == "") return $str;
		return $result;
	}
	
	public static function del_space($str){
		$result = "";
		$str = trim($str," ");
		$character = str_split($str);
		for ($i = 0;$i < strlen($str);$i++)
		{
			if($character[$i]!=" ")
				$result.= $character[$i];
			if($character[$i]==" " && $character[$i+1]!=" ")
				$result.= $character[$i];
		}
		return $result;
	}
	
	public static function update_index($row,$type,$action="insert")
	{
		if($type == 1)
			$index = Zend_Search_Lucene::open('index_product_en');
        if($type == 2)
			$index = Zend_Search_Lucene::open('index_news_en');
		if($type == 3)
			$index = Zend_Search_Lucene::open('index_qa_en');  
		if($action=="update" || $action=="delete")
		{
		    $removePath = $row['thing_id'];
		    $hits = $index->find('thing_id:' . $removePath);
		    foreach ($hits as $hit) {
		        $index->delete($hit->id);
		    }
		}
		if($action!="delete")
		{
		Zend_Search_Lucene_Analysis_Analyzer::setDefault(
               new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
        
       		$document = new Zend_Search_Lucene_Document();
	         
	        $document->addField(Zend_Search_Lucene_Field::text('thing_id', $row['thing_id'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('thing_code', $row['thing_code'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('category_id', $row['category_id'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('user_id', $row['user_id'], 'utf-8'));
	        
			$document->addField(Zend_Search_Lucene_Field::unIndexed('common_info', $row['common_info'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description', $row['description'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description_table', $row['description_table'], 'utf-8'));
	        
	        $description_lang=unserialize($row['short_description']);
			$title_lang=unserialize($row['title']);
			if(isset($description_lang['English']))
				$temp["short_description"]=$description_lang['English'];
			else
				$temp["short_description"]=$row['short_description'];
			if(isset($title_lang['English']))
				$temp["title"]=$title_lang['English'];
			else
				$temp["title"]=$row['title'];
								
	        $document->addField(Zend_Search_Lucene_Field::text('title', $temp["title"], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description', $temp["short_description"], 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::text('title_us', Util::loai_bo_dau_TV($temp["title"]), 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description_us', Util::loai_bo_dau_TV($temp["short_description"]), 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $row['avatar'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('price', $row['price'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('type', $row['type'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('views', $row['views'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('created_date', $row['created_date'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('update_date', $row['update_date'], 'utf-8'));
			 
	        $index->addDocument($document);
		}
	        $index->optimize(); 
	        
	    if($type == 1)
			$index = Zend_Search_Lucene::open('index_product_vi');
        if($type == 2)
			$index = Zend_Search_Lucene::open('index_news_vi');
		if($type == 3)
			$index = Zend_Search_Lucene::open('index_qa_vi'); 
		if($action=="update" || $action=="delete")
		{
		    $removePath = $row['thing_id'];
		    $hits = $index->find('thing_id:' . $removePath);
		    foreach ($hits as $hit) {
		        $index->delete($hit->id);
		    }
		}    
		if($action!="delete")
		{
    Zend_Search_Lucene_Analysis_Analyzer::setDefault(
               new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());

        	$document = new Zend_Search_Lucene_Document();
	         
	        $document->addField(Zend_Search_Lucene_Field::text('thing_id', $row['thing_id'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('thing_code', $row['thing_code'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('category_id', $row['category_id'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('user_id', $row['user_id'], 'utf-8'));
	        
			$document->addField(Zend_Search_Lucene_Field::unIndexed('common_info', $row['common_info'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description', $row['description'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description_table', $row['description_table'], 'utf-8'));
	        
	        $description_lang=unserialize($row['short_description']);
			$title_lang=unserialize($row['title']);
			if(isset($description_lang['Vietnamese']))
				$temp["short_description"]=$description_lang['Vietnamese'];
			else
				$temp["short_description"]=$row['short_description'];
			if(isset($title_lang['Vietnamese']))
				$temp["title"]=$title_lang['Vietnamese'];
			else
				$temp["title"]=$row['title'];
								
	        $document->addField(Zend_Search_Lucene_Field::text('title', $temp["title"], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description', $temp["short_description"], 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::text('title_us', Util::loai_bo_dau_TV($temp["title"]), 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description_us', Util::loai_bo_dau_TV($temp["short_description"]), 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $row['avatar'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('price', $row['price'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('type', $row['type'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('views', $row['views'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('created_date', $row['created_date'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('update_date', $row['update_date'], 'utf-8'));
			 
	        $index->addDocument($document);
		}
	        $index->optimize();
	}
	
	public static function create_index($data,$type){
		if($type == 1)
			$index = Zend_Search_Lucene::create('index_product_en');
        if($type == 2)
			$index = Zend_Search_Lucene::create('index_news_en');
		if($type == 3)
			$index = Zend_Search_Lucene::create('index_qa_en');  
    Zend_Search_Lucene_Analysis_Analyzer::setDefault(
               new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
       
	    foreach ($data as $row)
	    {
	        $document = new Zend_Search_Lucene_Document();
	         
	        $document->addField(Zend_Search_Lucene_Field::text('thing_id', $row['thing_id'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('thing_code', $row['thing_code'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('category_id', $row['category_id'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('user_id', $row['user_id'], 'utf-8'));
	        
			$document->addField(Zend_Search_Lucene_Field::unIndexed('common_info', $row['common_info'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description', $row['description'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description_table', $row['description_table'], 'utf-8'));
	        
	        $description_lang=unserialize($row['short_description']);
			$title_lang=unserialize($row['title']);
			if(isset($description_lang['English']))
				$temp["short_description"]=$description_lang['English'];
			else
				$temp["short_description"]=$row['short_description'];
			if(isset($title_lang['English']))
				$temp["title"]=$title_lang['English'];
			else
				$temp["title"]=$row['title'];
								
	        $document->addField(Zend_Search_Lucene_Field::text('title', $temp["title"], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description', $temp["short_description"], 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::text('title_us', Util::loai_bo_dau_TV($temp["title"]), 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description_us', Util::loai_bo_dau_TV($temp["short_description"]), 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $row['avatar'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('price', $row['price'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('type', $row['type'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('views', $row['views'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('created_date', $row['created_date'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('update_date', $row['update_date'], 'utf-8'));
			 
	        $index->addDocument($document);
	      }
	      
      $index->optimize(); 
      
      	if($type == 1)
			$index = Zend_Search_Lucene::create('index_product_vi');
        if($type == 2)
			$index = Zend_Search_Lucene::create('index_news_vi');
		if($type == 3)
			$index = Zend_Search_Lucene::create('index_qa_vi'); 
            
    Zend_Search_Lucene_Analysis_Analyzer::setDefault(
               new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
                
	    foreach ($data as $row)
	    {
	        $document = new Zend_Search_Lucene_Document();
	         
	        $document->addField(Zend_Search_Lucene_Field::text('thing_id', $row['thing_id'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('thing_code', $row['thing_code'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('category_id', $row['category_id'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('user_id', $row['user_id'], 'utf-8'));
	        
			$document->addField(Zend_Search_Lucene_Field::unIndexed('common_info', $row['common_info'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description', $row['description'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('description_table', $row['description_table'], 'utf-8'));
	        
	        $description_lang=unserialize($row['short_description']);
			$title_lang=unserialize($row['title']);
			if(isset($description_lang['Vietnamese']))
				$temp["short_description"]=$description_lang['Vietnamese'];
			else
				$temp["short_description"]=$row['short_description'];
			if(isset($title_lang['Vietnamese']))
				$temp["title"]=$title_lang['Vietnamese'];
			else
				$temp["title"]=$row['title'];
								
	        $document->addField(Zend_Search_Lucene_Field::text('title', $temp["title"], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description', $temp["short_description"], 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::text('title_us', Util::loai_bo_dau_TV($temp["title"]), 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('short_description_us', Util::loai_bo_dau_TV($temp["short_description"]), 'utf-8'));
	        
	        $document->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $row['avatar'], 'utf-8'));
	        $document->addField(Zend_Search_Lucene_Field::text('price', $row['price'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('type', $row['type'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('views', $row['views'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::unIndexed('created_date', $row['created_date'], 'utf-8'));
			$document->addField(Zend_Search_Lucene_Field::text('update_date', $row['update_date'], 'utf-8'));
			 
	        $index->addDocument($document);
	      }
	      
      $index->optimize(); 
	}
	
	public static function loai_bo_dau_TV($str){
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
				,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ");
				
		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D");
				
	$result = str_replace($marTViet,$marKoDau,$str);
	return $result;
	}
}